import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;


public class Grid {
	
	private int step;
	GridPanel panel;
	public Grid(int step, GridPanel panel){
		this.step=step;
		this.panel=panel;
	}
	
	public void drawGrid(Graphics g){
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		BasicStroke pen = new BasicStroke(1, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_MITER, 30);
		g2d.setColor(Color.LIGHT_GRAY);
		g2d.setStroke(pen);

		for (int x = 0; x < panel.getWidth(); x += step)
			g2d.drawLine(x, 0, x, panel.getHeight());
		for (int y = 0; y < panel.getHeight(); y += step)
			g2d.drawLine(0, y, panel.getWidth(), y);
	}
	
	public void setStep(int step){
		this.step=step;
	}
	
	public int round(int x){
		int less=x%step;
		int more=step-less;
		if(less<more)
			return x-less;
		else
			return x+more;
	}
	
	public Point round(Point p){
		return new Point(round(p.x),round(p.y));
	}
}
